package org.wpahs.ahn.rssfeed;

import android.text.Html;
import android.webkit.URLUtil;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;



/**
 * Class for processing RSS Feeds
 * Created by dgambone on 01/02/2015.
 */
public class Feed {
    private String titleImage;
    private String title;
    private String link;
    private String description;
    private List<FeedItem> items = new ArrayList<>();
    private int feedId;
    String image;
    private List<String> imageUrls = new ArrayList<>();


    public Feed(int feedId,Document feed){
        this.feedId=feedId;
        String name;
        String val;
        Node link;
        String url;
        Node image;
        Node type;
        NamedNodeMap attributes;

        if(feed!=null){

            NodeList nodes = null;
            NodeList itemsNodes;
            FeedItem item = new FeedItem(getFeedId());
            if(feed.getElementsByTagName("channel").getLength() > 0){
                nodes = feed.getElementsByTagName("channel").item(0).getChildNodes();
            }
            if(nodes==null){
                if(feed.getElementsByTagName("feed").getLength() > 0) {
                    nodes = feed.getElementsByTagName("feed").item(0).getChildNodes();
                }
            }
            for (int x=0; x<nodes.getLength(); x++){
                //Log.i("Node",nodes.item(x).getNodeName());
                Node node = nodes.item(x);
                name= node.getNodeName();
                val=node.getTextContent();
                if(name.equals("title")) {
                    setTitle(val);
                }else if(name.equals("image")){
                        //check to see if has a url child
                    if(node.hasChildNodes()){
                        NodeList titleImageNodes = node.getChildNodes();
                        for(int chIdx1 =0; chIdx1<titleImageNodes.getLength(); chIdx1++){
                            if(titleImageNodes.item(chIdx1).getNodeName().equals("url")){
                                this.titleImage = titleImageNodes.item(chIdx1).getTextContent();
                            }
                        }
                    }
                }else if(name.equals("description")){
                    setDescription(val);
                }else if(name.equals("link")) {
                    if (URLUtil.isValidUrl(val)) {
                        setLink(val);
                    } else {
                        link = node.getAttributes().getNamedItem("href");
                        if (link != null) {
                            url = link.getNodeValue();
                            if (URLUtil.isValidUrl(url)) {
                                setLink(url);
                            }
                        }
                    }

                }else if(name.equals("item") || name.equals(("entry"))){
                    item = new FeedItem(getFeedId());
                    itemsNodes = node.getChildNodes();
                    for (int y=0; y<itemsNodes.getLength(); y++) {

                        if(itemsNodes.item(y).getNodeName().equals("title")){
                            item.setTitle(itemsNodes.item(y).getTextContent());
                        }else if(itemsNodes.item(y).getNodeName().equals("description")) {
                            item.setDescription(itemsNodes.item(y).getTextContent());
                        }else if(itemsNodes.item(y).getNodeName().equals("summary")) {
                            item.setDescription(itemsNodes.item(y).getTextContent());
                        }else if(itemsNodes.item(y).getNodeName().equals("content")) {
                            item.setDescription(itemsNodes.item(y).getTextContent());
                        }else if(itemsNodes.item(y).getNodeName().equals("pubDate")) {
                            item.setPubDateFromFeed(itemsNodes.item(y).getTextContent());
                        }else if(itemsNodes.item(y).getNodeName().equals("published")) {
                            item.setPubDateFromFeed(itemsNodes.item(y).getTextContent());
                        }else if(itemsNodes.item(y).getNodeName().equals("media:thumbnail")) {
                            image = itemsNodes.item(y);
                            image = image.getAttributes().getNamedItem("url");
                            item.addThumbnailImage(image.getNodeValue());
                        }else if(itemsNodes.item(y).getNodeName().equals("enclosure")) {
                            attributes = itemsNodes.item(y).getAttributes();
                            if(attributes != null) {
                                type = attributes.getNamedItem("type");
                                if (type != null) {
                                    if (type.getNodeValue().contains("image")) {
                                        image = itemsNodes.item(y);
                                        image = image.getAttributes().getNamedItem("url");
                                        item.addThumbnailImage(image.getNodeValue());
                                    }
                                }
                            }
                        }else if(itemsNodes.item(y).getNodeName().equals("media:content")) {
                            image = itemsNodes.item(y);
                            image = image.getAttributes().getNamedItem("url");
                            item.addThumbnailImage(image.getNodeValue());
                        }else if(itemsNodes.item(y).getNodeName().equals("link")){
                            link=itemsNodes.item(y);
                            if(link.getAttributes() != null && link.getAttributes().getNamedItem("type") != null){
                                if(!link.getAttributes().getNamedItem("type").getNodeValue().contains("html")){
                                    continue;  //only want urls to html
                                }else{
                                    url = link.getAttributes().getNamedItem("href").getNodeValue();
                                }
                            }else{
                                url = link.getTextContent();
                            }
                            if(URLUtil.isValidUrl(url)){
                                item.setLink(url);
                            }else{
                                link = link.getAttributes().getNamedItem("href");
                                if(link != null){
                                    url = link.getNodeValue();
                                    if (URLUtil.isValidUrl(url)){
                                        item.setLink(url);
                                    }
                                }
                            }
                        }
                    }
                    this.items.add(item);

                }
            }
        }else{
            FillFeed();
        }
    }



    public void FillFeed(){
        this.setTitle("Test Feed");
        this.setDescription("This is a Faux Data");
        FeedItem i;
        for(int x=0; x<20; x++){
            i = new FeedItem(getFeedId());
            i.setTitle("Title " + String.valueOf(x));
            i.setDescription("Wow!, look what's happening here. Must be something amazing.");
            i.setLink("http://www.google.com");
            i.addDescriptionImage("http://alea04infl91.pgh.wpahs.org/test.gif");
            this.items.add(i);
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = Html.fromHtml(title).toString();
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        //find html and remove
        this.description = Html.fromHtml(description).toString();
    }

    public String getFeedImage(){
        return this.titleImage;
    }

    public List<FeedItem> getItems() {
        return items;
    }

    public int getFeedId() {
        return feedId;
    }

    public void setFeedId(int feedId) {
        this.feedId = feedId;
    }

    public String getImage() {
        if(imageUrls.size()>0){
            return imageUrls.get(0);
        }else{
            return null;
        }
    }

    public void setImage(String image) {
        this.image = image;
    }

    private void addImageURL(String url){
        if(URLUtil.isValidUrl(url)){
            imageUrls.add(url);
        }
    }


}
